package com.yzzrjy.util;

import java.util.Calendar;
import java.util.List;

public class MyDateUtil {
    public static int getAgeFromBirthday(String birthday){
        List<String> dateList = StringUtil.deFormatStringBySymbol(birthday, "-");
        if(dateList == null || dateList.size() != 3){
            return -1;
        }
        int borYear = Integer.parseInt(dateList.get(0));
        int borMonth = Integer.parseInt(dateList.get(1));
        int borDay = Integer.parseInt(dateList.get(2));

        Calendar calendar = Calendar.getInstance();
        int nowYear = calendar.get(Calendar.YEAR);
        int nowMonth = calendar.get(Calendar.MONTH);
        int nowDay = calendar.get(Calendar.DAY_OF_MONTH);

        if(nowYear < borYear ||
                (nowYear == borYear && nowMonth < borMonth) ||
                (nowYear == borYear && nowMonth == borMonth && nowDay < borDay)){
            return 0;
        }

        int age = nowYear - borYear;
        if(nowMonth < borMonth || (nowMonth == borMonth && nowDay < borDay)){
            age--;
        }
        return age;
    }
}
